<?php
namespace app\api\controller;
use app\Response;
use QL\QueryList;
use \Curl\Curl;
use \Curl\MultiCurl;
use think\Db;
use app\common;

class ChineseCharacters {
	public function index(){
		set_time_limit(0);
		//$zidian = self::hanwenxue();
		//$zidian = self::hanwenxueTwo();
		//$zidian = self::hanwenxueThree();
		//$zidian = self::cha();
		//$zidian = self::chaTwo();
		$zidian = self::checkJianti();
		Response::show(200,'ok',$zidian);
	}

	public static function checkJianti(){
		$total = Db::table('tbl_chinese_pinyin')->where('is_check',0)->count();

		$pagesize = 100;

		$editAll = 0;

		if($total){
			$allPage = ceil($total/$pagesize);
			for ($i=1; $i <= $allPage; $i++) { 
				$page = $i;
				$offsite = ($page - 1)*$pagesize;

				$limit = "{$offsite},{$pagesize}";
				$info = array();
				$info = Db::table('tbl_chinese_pinyin')->where('is_check',0)->limit($limit)->select();

				if($info){
					foreach ($info as $key => $val) {
						$pinyin_data = array();
						$character = isset($val['character'])?$val['character']:'';
						$pinyin = isset($val['pinyin'])?$val['pinyin']:'';

						//校验是否是简体字
						$is_jianti = self::check_jianti($character);

						if($is_jianti){
							$pinyin_data['character'] = $character;
							$pinyin_data['pinyin'] = $pinyin;
							$time = date('Y-m-d H:i:s');

							$pinyin_data['create_time'] = $time;
							$pinyin_data['update_time'] = $time;

							$key = array('character');
							$insert = common::addTbl('tbl_jianti_pinyin', $pinyin_data, $key);
							if($insert){
								$editAll++;
							}
						}
					}
				}
			}
		}
		return $editAll;
	}

	public static function check_jianti($character){
		$result = false;
		if($character){
			$info = Db::table('tbl_jianti')->where('character',$character)->find();
			if($info){
				$result = true;
			}
		}
		return $result;
	}

	public static function cha(){
		//新华字典，911查询
		$url = 'https://zidian.911cha.com/pinyin.html';
		$rule1 = array(
			//'letter'=>array('#pycontent>.clearfix>.divleft>a','name'),
			'spell' => array('.py>li>a','text'),
				// 获取当前切片下的a标签链接
			'link'=>array('.py>li>a','href')
			);

		//$ql = QueryList::get($url);
		$ql = QueryList::get($url,[],['verify' => false]);
		$html = $ql->getHtml();
		// 过程:设置HTML=>设置采集规则=>执行采集=>获取采集结果数据
		$data = QueryList::html($html)->rules($rule1)->query()->getData();

		$total = 0;
		if($data){
			foreach ($data as $key => $val) {
				$spell 	= isset($val['spell'])?$val['spell']:'';
				$link 	= isset($val['link'])?$val['link']:'';
				if(strstr($link, '/')){
					$arr = explode('/', $link);
					$link = isset($arr[1])?$arr[1]:$link;
					$link = 'https://zidian.911cha.com/'.$link;
				}
				$letter 	= !empty($spell)?strtoupper(substr($spell, 0, 1)):'';
				if($spell){
					$spell_id = self::addSpell($spell,$letter,$link,$url);
					if($spell_id){
						$total++;
					}
				}
			}
		}
		return $total;

	}
	//第二步，获取汉字及汉字详情的链接
	public static function chaTwo(){
		$web = 'https://zidian.911cha.com/pinyin.html';
		$info = Db::table('tbl_spell')->where('is_curl',0)->where('web', $web)->select();
		$rule2 = array(
			//'letter'=>array('#pycontent>.clearfix>.divleft>a','name'),
			'txt' => array('.zi>li>a','text'),
			// 获取当前切片下的a标签链接
			'link'=>array('.zi>li>a','href')
			);
		$total = 0;
		if($info){
			foreach ($info as $key => $val) {
				$spell_id 	= isset($val['spell_id'])?$val['spell_id']:'';
				$spell 		= isset($val['spell'])?$val['spell']:'';
				$letter 		= isset($val['letter'])?$val['letter']:'';
				$link 		= isset($val['link'])?$val['link']:'';
				$web 		= isset($val['web'])?$val['web']:'';
				if($link){
					$ql = QueryList::get($link,[],['verify' => false]);
					$html = $ql->getHtml();
					// 过程:设置HTML=>设置采集规则=>执行采集=>获取采集结果数据
					$data = QueryList::html($html)->rules($rule2)->query()->getData();
					var_dump($data);die;
					if($data){
						foreach ($data as $k => $v) {
							$link = isset($v['link'])?$v['link']:'';
							$txt  =  isset($v['txt'])?$v['txt']:'';
							if($txt){
								$txt_Arr = explode('画', $txt);
								$character = isset($txt_Arr[1])?trim($txt_Arr[1]):'';

								$character_id = self::addCharacter($character, $spell, $letter, $link);
								if($character_id){
									$total++;
								}
							}
						}
					}
					//修改链接使用状态
					$update = Db::table('tbl_spell')->where('spell_id', $spell_id)->update(['is_curl' => 1]);
				}
			}
		}
		return $total;
	}
	public static function baidu(){		
		$total = 0;
		
		Db::table('tbl_chinese_characters')->where('is_baidu_pinyin',0)->chunk(100, function($chinese_character) {
				$total = 0;
				$rule5 = array(
					//'letter'=>array('#pycontent>.clearfix>.divleft>a','name'),
					'pronounce' => array('.pronounce>span>b','text'),
					'radical' => array('#radical>span','text'),
					'stroke_count' => array('#stroke_count>span','text'),
					'wubi' => array('#wubi>span','text'),
				);
				$baidu = "http://hanyu.baidu.com/zici/s?wd=";
				foreach ($chinese_character as $val) {
					$character_id 	= isset($val['character_id'])?$val['character_id']:'';
					$character 	= isset($val['character'])?$val['character']:'';

					$url = $baidu.$character;

					$ql = QueryList::get($url);
										$html = $ql->getHtml();
					// 过程:设置HTML=>设置采集规则=>执行采集=>获取采集结果数据
					$data = QueryList::html($html)->rules($rule5)->query()->getData();
					if($data){
						$pinyin_Arr = array();
						$radical = '';
						$stroke_count = '';
						$wubi = '';
						foreach ($data as $m => $o) {
							$pronounce 	= isset($o['pronounce'])?trim($o['pronounce']):'';
							if($m == 0){
								$radical 	= isset($o['radical'])?$o['radical']:'';
								$stroke_count 	= isset($o['stroke_count'])?$o['stroke_count']:'';
								$wubi 		= isset($o['wubi'])?$o['wubi']:'';
							}
							!empty($pronounce)?($pinyin_Arr[] = $pronounce):'';
						}
						$pinyin = !empty($pinyin_Arr)?implode(',', $pinyin_Arr):'';

						$updata = array(
							'pinyin' => $pinyin,
							'radical' => $radical,
							'stroke' => $stroke_count,
							'wubi' => $wubi,
							'is_baidu_pinyin' => 1
							);
						$update = self::updateCharacter($character_id,$updata);
						if($update){
							$total++;
						}
					}
				}
				return $total;
		}, 'character');
		return $total;
	}

	//从百度汉语获取拼音
	public static function baidupinyin(){
		$limt = rand(90,120);
		$info = Db::table('tbl_chinese_characters')->where('is_baidu_pinyin',0)->limit($limt)->select();

		$rule5 = array(
			//'letter'=>array('#pycontent>.clearfix>.divleft>a','name'),
			'pronounce' => array('.pronounce>span>b','text'),
			'radical' => array('#radical>span','text'),
			'stroke_count' => array('#stroke_count>span','text'),
			'wubi' => array('#wubi>span','text'),
			);
		$total = 0;
		if($info){
			$baidu = "http://hanyu.baidu.com/zici/s?wd=";
			foreach ($info as $key => $val) {
				$character_id 	= isset($val['character_id'])?$val['character_id']:'';
				$character 	= isset($val['character'])?$val['character']:'';

				$url = $baidu.$character;

				$ql = QueryList::get($url);
				$html = $ql->getHtml();
				// 过程:设置HTML=>设置采集规则=>执行采集=>获取采集结果数据
				$data = QueryList::html($html)->rules($rule5)->query()->getData();

				if($data){
					$pinyin_Arr = array();
					$radical = '';
					$stroke_count = '';
					$wubi = '';
					foreach ($data as $m => $o) {
						$pronounce 	= isset($o['pronounce'])?trim($o['pronounce']):'';
						if($m == 0){
							$radical 	= isset($o['radical'])?$o['radical']:'';
							$stroke_count 	= isset($o['stroke_count'])?$o['stroke_count']:'';
							$wubi 		= isset($o['wubi'])?$o['wubi']:'';
						}
						!empty($pronounce)?($pinyin_Arr[] = $pronounce):'';
					}
					$pinyin = !empty($pinyin_Arr)?implode(',', $pinyin_Arr):'';

					$updata = array(
						'pinyin' => $pinyin,
						'radical' => $radical,
						'stroke' => $stroke_count,
						'wubi' => $wubi,
						'is_baidu_pinyin' => 1
						);
					$update = self::updateCharacter($character_id,$updata);
					if($update){
						$total++;
					}
				}
				//释放资源
				$ql->destruct();
			}
		}
		return $total;
	}

	public static function updateCharacter($character_id,$data=array()){
		$update = false;
		//插入拼音
		if($character_id && $data){
			$update =  Db::table('tbl_chinese_characters')->where('character_id', $character_id)->update($data);
		}
		return $update;
	}

	//第一步，获取不带音标的拼音及其链接
	public static function hanwenxue(){
		//汉文学网,在线新华字典
		$url = 'http://zd.hwxnet.com/pinyin.html';

		$rule1 = array(
			//'letter'=>array('#pycontent>.clearfix>.divleft>a','name'),
			'spell' => array('.divright>a','text'),
				// 获取当前切片下的a标签链接
			'link'=>array('.divright>a','href')
			);

		$ql = QueryList::get($url);
		$html = $ql->getHtml();
		// 过程:设置HTML=>设置采集规则=>执行采集=>获取采集结果数据
		$data = QueryList::html($html)->rules($rule1)->query()->getData();
		
		$total = 0;
		if($data){
			foreach ($data as $key => $val) {
				$spell 	= isset($val['spell'])?$val['spell']:'';
				$link 	= isset($val['link'])?$val['link']:'';
				$letter 	= !empty($spell)?strtoupper(substr($spell, 0, 1)):'';
				if($spell){
					$spell_id = self::addSpell($spell,$letter,$link,$url);
					if($spell_id){
						$total++;
					}
				}
			}
		}
		return $total;
	}

	//第二步，获取汉字及汉字详情的链接
	public static function hanwenxueTwo(){
		$info = Db::table('tbl_spell')->where('is_curl',0)->select();
		$rule2 = array(
			//'letter'=>array('#pycontent>.clearfix>.divleft>a','name'),
			'txt' => array('.fontbox','text'),
				// 获取当前切片下的a标签链接
			'link'=>array('.fontbox','href')
			);
		$total = 0;
		if($info){
			foreach ($info as $key => $val) {
				$spell_id 	= isset($val['spell_id'])?$val['spell_id']:'';
				$spell 		= isset($val['spell'])?$val['spell']:'';
				$letter 		= isset($val['letter'])?$val['letter']:'';
				$link 		= isset($val['link'])?$val['link']:'';
				$web 		= isset($val['web'])?$val['web']:'';
				if($link){
					$ql = QueryList::get($link);
										$html = $ql->getHtml();
					// 过程:设置HTML=>设置采集规则=>执行采集=>获取采集结果数据
					$data = QueryList::html($html)->rules($rule2)->query()->getData();
					if($data){
						foreach ($data as $k => $v) {
							$link = isset($v['link'])?$v['link']:'';
							$txt  =  isset($v['txt'])?$v['txt']:'';
							if($txt){
								$txt_Arr = explode('画', $txt);
								$character = isset($txt_Arr[1])?trim($txt_Arr[1]):'';

								$character_id = self::addCharacter($character, $spell, $letter, $link);
								if($character_id){
									$total++;
								}
							}
						}
					}
					//修改链接使用状态
					$update = Db::table('tbl_spell')->where('spell_id', $spell_id)->update(['is_curl' => 1]);
				}
			}
		}
		return $total;
	}
	//第三步，获取汉字的拼音
	public static function hanwenxueThree(){
		$rule3 = array(
			'txt' => array('#sub_con>.introduce>div>.pinyin','text'),
			);
		$info = Db::table('tbl_chinese_characters')->where('pinyin',null)->select();
		//$info = Db::table('tbl_chinese_characters')->where('character_id','>',6346)->select();
		$total = 0;
		if($info){
			foreach ($info as $key => $val) {
				$character_id 	= isset($val['character_id'])?$val['character_id']:'';
				$link 		= isset($val['link'])?$val['link']:'';
				$pinyin         	= ''; 
				if($link){
					$ql = QueryList::get($link);
										$html = $ql->getHtml();
					// 过程:设置HTML=>设置采集规则=>执行采集=>获取采集结果数据
					$html = str_replace('&nbsp;','',$html);
					$data = QueryList::html($html)->rules($rule3)->query()->getData();
					$pinyin_Arr = array();
					if($data){
						foreach ($data as $k => $v) {
							$txt = isset($v['txt'])?trim($v['txt']):'';
							!empty($txt)?($pinyin_Arr[] = $txt):'';	

						}
					}
					$pinyin = !empty($pinyin_Arr)?(implode(',', $pinyin_Arr)):'';
				}
				//插入拼音
				$update =  Db::table('tbl_chinese_characters')->where('character_id', $character_id)->update(['pinyin' => $pinyin]);
				if($update){
					$total++;
				}
			}
		}
		return $total;
	}
	//去除拼音中的空格
	public function del_space(){
		//$info = Db::table('tbl_chinese_characters')->where('del_space',0)->select();
		$info = Db::table('tbl_chinese_characters')->where('character_id',6376)->select();
		if($info){
			foreach ($info as $key => $val) {
				$character_id 	= isset($val['character_id'])?$val['character_id']:'';
				$pinyin 	= isset($val['pinyin'])?$val['pinyin']:'';
				$pinyin = str_replace('&nbsp;','',$pinyin);
				//$pinyin = preg_replace('/&nbsp;/is','',$pinyin);
				
				//$pinyin = trim($pinyin,chr(0xc2) . chr(0xa0));
				/*$txt = strip_tags($txt);
				$txt =  str_replace('&nbsp;', '', $txt);  
				$txt = str_replace(array("&nbsp;","&amp;nbsp;","\t","\r\n","\r","\n"),array("","","","","",""),$txt);*/
				//$txt = trim(html_entity_decode($txt),chr(0xc2).chr(0xa0));  
				
				//$txt = preg_replace('/^(&nbsp;|＼s)*|(&nbsp;|＼s)*$/', '', $txt);  

				
				//!empty($txt)?(str_replace('&nbsp;','',$txt)):'';
				var_dump($pinyin,strlen($pinyin));die;
			}
		}
	}


	//第四步，获取部首
	public  function hanwenxueFour(){
		$rule4 = array(
			'txt' => array('#sub_con>.introduce>.label>div','text'),
			'bihua' => array('#sub_con>.introduce>.label>.spwid80','text'),
			);
		$info = Db::table('tbl_chinese_characters')->where('radical',null)->limit(10)->select();
		$total = 0;
		if($info){
			foreach ($info as $key => $val) {
				$character_id 	= isset($val['character_id'])?$val['character_id']:'';
				$link 		= isset($val['link'])?$val['link']:'';
				$pinyin         	= ''; 
				if($link){
					$ql = QueryList::get($link);
										$html = $ql->getHtml();
					// 过程:设置HTML=>设置采集规则=>执行采集=>获取采集结果数据
					$html = str_replace('&nbsp;','',$html);
					$data = QueryList::html($html)->rules($rule4)->query()->getData();
					var_dump($data);die;
					$pinyin_Arr = array();
					if($data){
						foreach ($data as $k => $v) {
							$txt = isset($v['txt'])?trim($v['txt']):'';
							!empty($txt)?($pinyin_Arr[] = $txt):'';						
						}
					}
					$pinyin = !empty($pinyin_Arr)?(implode(',', $pinyin_Arr)):'';
				}
				//插入拼音
				//$update =  Db::table('tbl_chinese_characters')->where('character_id', $character_id)->update(['pinyin' => $pinyin]);
				/*if($update){
					$total++;
				}*/
			}
		}
		return $total;
	}

	public static function addSpell($spell,$letter,$link,$web){
		//校验是否已录入
		$is_save = Db::table('tbl_spell')->where('spell',$spell)->find();

		if($is_save){
			$spell_id = isset($is_save['spell_id'])?$is_save['spell_id']:'';
		}else{
			$data = array(
				'spell' 		=> $spell,
				'letter' 		=> $letter,
				'link' 		=> $link,
				'web' 		=> $web,
				'create_time' 	=>  date('Y-m-d H:i:s'),
				'update_time' 	=>  date('Y-m-d H:i:s')
				);
			$spell_id = Db::table('tbl_spell')->insertGetId($data);
		}
		return $spell_id;
	}

	public static function addCharacter($character, $spell, $letter, $link){
		//校验是否已录入
		$is_save = Db::table('tbl_chinese_characters')->where('character',$character)->find();

		if($is_save){
			$character_id = isset($is_save['character_id'])?$is_save['character_id']:'';
		}else{
			$data = array(
				'character' 	=> $character,
				'spell' 		=> $spell, 
				'letter' 		=> $letter,
				'link' 		=> $link,
				'create_time' 	=>  date('Y-m-d H:i:s'),
				'update_time' 	=>  date('Y-m-d H:i:s')
				);
			$character_id = Db::table('tbl_chinese_characters')->insertGetId($data);
		}
		return $character_id;
	}
	/**
	 * [queryListEncodeing phpQuery认不清目标网页是什么编码，即找不到meta属性的时候，会默认转换成iso-8859-1的格式,所以乱码么，需要先从iso-8859-1的格式转换成utf-8的格式，再转换成GBK的格式]
	 * @param  string $str [需要转化的内容]
	 * @return [type]      [description]
	 */
	public static function queryListEncodeing($str=''){
		if($str){
			$str = mb_convert_encoding($str,'ISO-8859-1','UTF-8');
			$str = mb_convert_encoding($str,'UTF-8','GBK');
		}
		return $str;
	}
	/**
	 * [encoding 编码转化]
	 * @param  string $str           [要转化的字符]
	 * @param  string $encoding_to   [要转化的目的编码]
	 * @param  string $encoding_from [原编码]
	 * @return [type]                [description]
	 */
	public static function encoding($str='', $encoding_to="UTF-8", $encoding_from="GBK"){
		if($str){
			//已知原编码为encoding_from，转换为encoding_to
			$str = mb_convert_encoding($str, $encoding_to, $encoding_from); 
		}
		return $str;
	}
}